<template>
	<view>
		<navbar :autoBack='true' title="修改密码"></navbar>
		<view class="con u-flex u-flex-column u-flex-items-center">
			<!-- <view class="logo">

			</view> -->
			<!-- <view class="title">
				
			</view> -->
			<view class="form">
				<!-- <view class="inpbox">
					<image src="/static/login/phone.png" class="inpbox__phone" mode=""></image>
					<input class="inpbox__inp" type="text" v-model="form.username" placeholder="请输入用户名" />
				</view>
				<view class="inpbox">
					<image src="/static/login/phone.png" class="inpbox__phone" mode=""></image>
					<input class="inpbox__inp" type="number" v-model="form.mobile" maxlength="11" placeholder="请输入电话号码" />
				</view> -->
				<view class="inpbox">
					<image src="/static/login/pws.png" class="inpbox__pws" mode=""></image>
					<input class="inpbox__inp" type="password" placeholder="请输入密码" v-model='form.newpassword' />
				</view>
				<view class="inpbox">
					<image src="/static/login/pws.png" class="inpbox__pws" mode=""></image>
					<input class="inpbox__inp" type="password" placeholder="请再次输入密码" v-model='repassword' />
				</view>
				<view class="inpbox">
					<image src="/static/login/yzm.png" class="inpbox__yzm" mode=""></image>
					<input class="inpbox__inp" type="number" placeholder="请输入验证码" v-model="form.capthca" maxlength="6" />
					<u-button color="#3262FD" @tap="getCode"
						:customStyle='{width:"200rpx",height:"84rpx",borderRadius:"15rpx",marginRight:"10rpx",fontSize:"24rpx"}'>{{tips}}</u-button>
				</view>
				
				<!-- <view class="inpbox">
					<image src="/static/login/yqm.png" class="inpbox__yqm" mode=""></image>
					<input class="inpbox__inp" type="number" placeholder="请输入推荐码" />
				</view> -->
			</view>
			<u-button text="确认修改" color="#3262FD" size="large" @click="submit"
				:customStyle='{width:"606rpx",height:"111rpx",borderRadius:"15rpx",marginTop:"30rpx"}'></u-button>
		</view>
		<u-code change-text='Xs重新获取' :seconds="seconds" ref="uCode"
			@change="codeChange"></u-code>
	</view>
</template>

<script>
	import user from '@/api/_user.js'
	export default {

		data() {
			return {
				tips: '',
				// refCode: null,
				seconds: 60,
				form:{
					newpassword:'',
					capthca:'',
					mobile:uni.getStorageSync('88ffuserinfo').mobile
				},
				repassword:''
			};
		},
		onReady() {
			// 注意这里不能将一个组件赋值给data的一个变量，否则在微信小程序会
			// 造成循环引用而报错，如果你想这样做，请在非data中定义refCode变量
			this.refCode = this.$refs.uCode;
		},
		methods: {
			codeChange(text) {
				this.tips = text;
			},
			submit(){
				let {newpassword,capthca}=this.form
				
				if(!capthca){
					uni.$u.toast('请输入验证码');
					return
				}
				if(!newpassword){
					uni.$u.toast('请输入密码');
					return
				}
				if(newpassword!=this.repassword){
					uni.$u.toast('两次密码不一致');
					return
				}
				user.resetpwd(this.form).then(res=>{
					if(res.data.code==1){
						uni.$u.toast('密码修改成功,请重新登录');
						uni.removeStorageSync('88ffuserinfo')
						uni.removeStorageSync('88ftoken')
						setTimeout(()=>{
							uni.reLaunch({
								url:'/pages/index/login'
							})
						},1000)
					}
				})
			},
			getCode() {
				if(!this.form.mobile){
					uni.$u.toast('请输入手机号');
					return
				}
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					user.send({mobile:this.form.mobile,event:'resetpwd'}).then(res=>{
						uni.hideLoading();
						if(res.data.code==1){
							uni.$u.toast('验证码已发送');
							this.$refs.uCode.start();
						}
					})
					// setTimeout(() => {
					// 	uni.hideLoading();
					// 	// 这里此提示会被this.start()方法中的提示覆盖
					// 	uni.$u.toast('验证码已发送');
					// 	// 通知验证码组件内部开始倒计时
					// 	this.$refs.uCode.start();
					// }, 2000);
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: #F0F3F7;
	}
	.con {
		padding-top: 50rpx;
		position: relative;
		padding-bottom: 120rpx;
	}

	.logo {
		width: 224rpx;
		height: 225rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 7rpx 40rpx 0rpx rgba(72, 139, 254, 0.18);
		border-radius: 90rpx;
	}

	.title {
		width: 326rpx;
		height: 39rpx;
		font-family: PingFang SC;
		font-weight: bold;
		font-size: 41rpx;
		color: #1E1E1E;
		line-height: 41rpx;
		margin: 70rpx 0 79rpx;
	}

	.inpbox {
		width: 606rpx;
		height: 107rpx;
		background: #F6F9FF;
		border-radius: 15rpx;
		display: flex;
		align-items: center;
		margin-bottom: 33rpx;

		&__pws {
			width: 32rpx;
			height: 37rpx;
			margin-left: 46rpx;
			margin-right: 28rpx;
		}

		&__phone {
			width: 28rpx;
			height: 35rpx;
			margin: 0 30rpx 0 46rpx;
		}

		&__yzm {
			width: 28rpx;
			height: 33rpx;
			margin: 0 30rpx 0 48rpx;
		}

		&__yqm {
			width: 32rpx;
			height: 33rpx;
			margin: 0 28rpx 0 48rpx;
		}

		&__inp {
			flex: 1;
			font-family: PingFang SC;
			font-size: 28rpx;
			color: #333;
		}

		.uni-input-placeholder {
			color: #A1A4AA;
			font-family: PingFang SC;
			font-size: 28rpx;
		}
	}

	.tips {
		width: 606rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 26rpx;
		color: #A9ADB8;
		line-height: 26rpx;
		margin-top: 34rpx;
		margin-bottom: 100rpx;
	}

	.tips1 {
		font-family: PingFang SC;
		font-weight: 500;
		font-size: 28rpx;
		color: #A1A4AA;
		line-height: 28rpx;
	}

	.wx {
		width: 112rpx;
		height: 112rpx;
		margin-top: 29rpx;
	}

	.u-button--normal {
		padding: 0;
	}
</style>